package com.paomi.client.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.classify.model.SearchGetBeaan;
import com.paomi.client.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SearchPopupMenu extends PopupWindow{
    private Activity activity;
    private View popView;
    private RecyclerView recycler_pop;
    private OnClassifyClickListener clickListener;
    Typeface iconfont;

    public SearchPopupMenu(Activity activity,List<SearchGetBeaan> strings) {
        super(activity);
        iconfont = Typeface.createFromAsset(activity.getAssets(), "iconfont.ttf");
        this.activity = activity;
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        popView = inflater.inflate(R.layout.search_pop, null);// 加载菜单布局文件
        this.setContentView(popView);// 把布局文件添加到popupwindow中
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);// 设置菜单的宽度（需要和菜单于右边距的距离搭配，可以自己调到合适的位置）
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);// 获取焦点
        this.setTouchable(true); // 设置PopupWindow可触摸
        this.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);

        recycler_pop = popView.findViewById(R.id.recycler_pop);
        recycler_pop.setLayoutManager(UIUtils.getRecyclerViewManager(false,activity));
        SearchListAdapter listAdapter=new SearchListAdapter(activity,iconfont);
        recycler_pop.setAdapter(listAdapter);
        listAdapter.setData(strings);
        listAdapter.OnItemClick(new OnItemClick() {
            @Override
            public void setOnItemClick(View view, int pos) {
                dismiss();
                if(clickListener!=null){
                    clickListener.onGetDataClick(view,pos);
                }
            }
        });
    }

    /**
     * 改变背景颜色
     */
    private void darkenBackground(Float bgcolor) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);

    }

    /**
     * 设置显示的位置
     *
     * @param resourId 这里的x,y值自己调整可以
     */
    public void showLocation(int resourId) {
        showAsDropDown(activity.findViewById(resourId), dip2px(activity, 30),
                dip2px(activity, 5));
    }

    // dip转换为px
    public int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
    /**
     * 获取屏幕宽度(px)
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    // 点击监听接口
    public interface OnClassifyClickListener {
        void onGetDataClick(View view,int pos);

    }

    // 设置监听
    public void setOnClassifyClickListener(OnClassifyClickListener onItemClickListener) {
        this.clickListener = onItemClickListener;
    }

    class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.ViewHolder> {
        Context context;
        List<SearchGetBeaan> mlist = new ArrayList<>();
        OnItemClick onClickListener;
        private Typeface mType;

        public void OnItemClick(OnItemClick onClickListener) {
            this.onClickListener = onClickListener;
        }


        public SearchListAdapter(Context context, Typeface typeface) {
            this.context = context;
            this.mType = typeface;
        }

        public void setData(List<SearchGetBeaan> list) {
            this.mlist = list;
        }

        @Override
        public SearchListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new SearchListAdapter.ViewHolder(LayoutInflater.from(context).inflate(R.layout.search_pop_item, parent, false));
        }

        @Override
        public void onBindViewHolder(final SearchListAdapter.ViewHolder holder, final int position) {
            SearchGetBeaan ticket = mlist.get(position);
            holder.tv_get.setText(ticket.getName());
            holder.tv_hot.setText(ticket.getText());
            holder.tv_hot.setTypeface(mType);
            holder.ll_hot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onClickListener!=null){
                        onClickListener.setOnItemClick(holder.ll_hot,position);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mlist.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.ll_hot)
            LinearLayout ll_hot;
            @BindView(R.id.tv_hot)
            TextView tv_hot;
            @BindView(R.id.tv_get)
            TextView tv_get;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

    public interface OnItemClick {
        void setOnItemClick(View view, int pos);
    }
}
